package com.mainworks.template.config;

import com.mainworks.commons.impl.PassWordEncrypted;
import com.mainworks.template.service.security.SecurityUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Security配置类
 * @Author: HuangMinJie
 * @CreateDate: 2021/10/31 11:16
 * @Description:
 * @Version: 1.0.0.0
 * @ModifiedBy:
 * @ModifiedDate:
 * @ModifiedDescription:
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityUserDetailsService userDetailsService;



    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }

    /**
     * 自定义密码 校验类
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new PassWordEncrypted();
    }


    /**
     * 自定义的 UserDetailsService
     *@Author: HuangMinJie
     *@CreateDate:  2021/10/31 12:41
     *@Description:
     */
    @Override
    @Bean
    public UserDetailsService userDetailsService(){
        /*return username -> {
            Admin admin = adminService.getAdminByUserName(username);
            if (null!=admin){
                admin.setRoles(adminService.getRoles(admin.getId()));
                return admin;
            }
            throw new UsernameNotFoundException("用户名或密码不正确");
        };*/
        return userDetailsService;
    }
}
